/* Assignment Notebook
 * Created By: Amanda Rush
 * www.AmandaRush.com
 * Version 1.1
 * Release: 4/14/2011
 */

package com.captainkat.asn.view;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import java.util.Calendar;
import java.util.TimeZone;
import android.widget.TextView;
import java.text.SimpleDateFormat;

import com.captainkat.asn.bo.EventHolder;
import com.captainkat.asn.calendar.CalendarManager;
import com.captainkat.asn.R;

import android.graphics.Typeface;

//Date Picker
import android.app.Dialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class AssignmentEdit extends Activity implements OnClickListener {
	private TextView enterDate;
	private TextView enterTime;
	private TextView enterEndTime;
	private TextView title;
	private TextView desc;
	private TextView save;
	private TextView cancel;
	private TextView delete;
	private Calendar currentDate;
	private TimeZone timezone;
	private SimpleDateFormat dateFormat;
	private SimpleDateFormat timeFormat;
	private Calendar newEventDate;
	private Calendar newEventEndDate;
	private Boolean editing;
	private int eid;
	private CheckBox allday;
	private Typeface font;

	private CalendarManager calendarManager;

	//set the calendar id
	private String calendarId = "-1";

	//Date Picker
	static final int DATE_DIALOG_ID = 0;
	static final int START_TIME_DIALOG_ID = 1;
	static final int END_TIME_DIALOG_ID = 2;
	
	//event instances
	private EventHolder eventHolder;
	
	public AssignmentEdit() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setRequestedOrientation(1);  
		setContentView(R.layout.editexisting);
		
		font = Typeface.createFromAsset(getAssets(), "dandelion_in_the_spring.ttf");  
		
		eventHolder= new EventHolder();
		
		//get today's date on the calendar
		currentDate = Calendar.getInstance();
		//set up a new event date
		newEventDate=Calendar.getInstance();
		newEventEndDate=Calendar.getInstance();
		
		
		//GET Passed-in-date OUT OF BUNDLE
		Bundle b = this.getIntent().getExtras();
		editing = b.getBoolean("editing");
		calendarId = b.getString("calendarId");
		currentDate.setTimeInMillis(b.getLong("time"));
		eid = b.getInt("eid");
		
		timezone = TimeZone.getDefault();
		currentDate.setTimeZone(timezone);
		
		//set up a new event date
		newEventDate.setTimeZone(timezone);
		newEventEndDate.setTimeZone(timezone);
		
		//set up date formats
		dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		timeFormat = new SimpleDateFormat("hh:mm a");
		
		//set up View objects and ClickListeners
		enterDate = (TextView) findViewById(R.id.edate);
		enterDate.setOnClickListener(this);
		enterDate.setTypeface(font);
		
		enterTime = (TextView) findViewById(R.id.etime);
		enterTime.setOnClickListener(this);
		enterTime.setTypeface(font);
		
		enterEndTime = (TextView) findViewById(R.id.eendtime);
		enterEndTime.setOnClickListener(this);
		enterEndTime.setTypeface(font);
		
		
		title = (TextView) findViewById(R.id.eenterTitle);
		title.setOnClickListener(this);
		title.setTypeface(font);
		
		desc = (TextView) findViewById(R.id.eenterDesc);
		desc.setOnClickListener(this);
		desc.setTypeface(font);

		
		save = (TextView) findViewById(R.id.esave);
		save.setOnClickListener(this);
		save.setTypeface(font);

		
		cancel = (TextView) findViewById(R.id.ecancel);
		cancel.setOnClickListener(this);
		cancel.setTypeface(font);

		
		delete = (TextView) findViewById(R.id.edelete);
		delete.setOnClickListener(this);
		delete.setTypeface(font);

		
		allday = (CheckBox)findViewById(R.id.allday);
		allday.setOnClickListener(this);
		allday.setTypeface(font);
		//hide the allday checker until later version that doesn't screw this up
		allday.setVisibility(View.INVISIBLE);

		//Put the current date on the screen
		enterDate.setText(dateFormat.format(currentDate.getTime()));
		enterTime.setText(timeFormat.format(currentDate.getTime()));
		enterEndTime.setText(timeFormat.format(currentDate.getTime()));
		
		calendarManager = new CalendarManager();
		calendarManager.init(this);
		
		drawScreen();
	}
	
	@Override
	public void onClick(View v)  {
		
		Bundle b;
	switch (v.getId()) { //what to do when you press one of the buttons
		
		case R.id.edate:
			showDialog(DATE_DIALOG_ID);
			break;
		case R.id.etime:
			showDialog(START_TIME_DIALOG_ID);
			break;
		case R.id.eendtime:
			showDialog(END_TIME_DIALOG_ID);
			break;
	
			
		case R.id.esave:
			
			EventHolder e = new EventHolder();
			
			e.setDescription(desc.getText().toString());
			e.setDtstart(newEventDate.getTime().getTime());
			e.setDtend(newEventEndDate.getTime().getTime());
			e.setTitle(title.getText().toString());
			e.setEid(Integer.toString(eid));
			
			if(allday.isChecked()) {
				e.setAllDayEvent(1);
			} else {
				e.setAllDayEvent(0);
			}
			
			if (editing) {
				e.setCalendarId(eventHolder.getCalendarId());
				calendarManager.updateEvent(e);
			} else {
				e.setCalendarId(calendarId);
				calendarManager.insertEvent(e);
			}
			
			Intent saveAsn = new Intent(v.getContext(), DailyView.class);
			b = new Bundle();
			b.putLong("time",newEventDate.getTimeInMillis());
			saveAsn.putExtras(b);
            startActivity(saveAsn);
            finish();
			break;
		case R.id.allday: 
			if (allday.isChecked()) {
				enterTime.setText("All");
				enterEndTime.setText("Day");
				enterTime.setEnabled(false);
				enterEndTime.setEnabled(false);
	
			} else {
				enterTime.setText(timeFormat.format(newEventDate.getTime()));
				enterEndTime.setText(timeFormat.format(newEventEndDate.getTime()));
				enterTime.setEnabled(true);
				enterEndTime.setEnabled(true);

			}
			break;
		case R.id.ecancel:
			//go back
			Intent cancelAsn = new Intent(v.getContext(), DailyView.class);
			//pass along the time: so it goes back to the date we were on
			b = new Bundle();
			b.putLong("time",currentDate.getTimeInMillis());
			cancelAsn.putExtras(b);
            startActivity(cancelAsn);
            finish();
            break;
		case R.id.edelete:
			calendarManager.deleteEvent(eventHolder);
			
			Intent deleteAsn = new Intent(v.getContext(), DailyView.class);
			b = new Bundle();
			//make it go back to the date we were on
			b.putLong("time", currentDate.getTimeInMillis());
			deleteAsn.putExtras(b);
			startActivity(deleteAsn);
			finish();
			break;
			
	}
	}
	
	
	

	
public void drawScreen() {		
	if (allday.isChecked()) {
		enterTime.setText("All");
		enterEndTime.setText("Day");
		enterTime.setEnabled(false);
		enterEndTime.setEnabled(false);
	} else {
		enterTime.setText(timeFormat.format(newEventDate.getTime()));
		enterEndTime.setText(timeFormat.format(newEventEndDate.getTime()));
		enterTime.setEnabled(true);
		enterEndTime.setEnabled(true);
	}
	
	if (editing)	{
		calendarManager.createEventsListforDate(currentDate);
		eventHolder = calendarManager.getEventByIdFromManagedList(Integer.toString(eid));
		
		newEventDate.setTimeInMillis(eventHolder.getDtstart());
		newEventEndDate.setTimeInMillis(eventHolder.getDtend());
		
		//show dates
		enterDate.setText(dateFormat.format(newEventDate.getTime()));
		
		if (eventHolder.getAllDayEvent() == 1) {
			enterTime.setText("All");
			enterEndTime.setText("Day");
			enterTime.setEnabled(false);
			enterEndTime.setEnabled(false);
			allday.setChecked(true);
		} else {
			enterTime.setText(timeFormat.format(newEventDate.getTime()));
			enterEndTime.setText(timeFormat.format(newEventEndDate.getTime()));
			enterTime.setEnabled(true);
			enterEndTime.setEnabled(true);
			allday.setChecked(false);
		}
		title.setText(eventHolder.getTitle());	
		desc.setText(eventHolder.getDescription());
	} else {
		delete.setVisibility(View.INVISIBLE);

		title.setText("");
		desc.setText("");
		
		//put the date on the screen
		enterDate.setText(dateFormat.format(currentDate.getTime()));
		enterTime.setText(timeFormat.format(currentDate.getTime()));
		enterEndTime.setText(timeFormat.format(currentDate.getTime()));
	}
}

public void changeIntentTime() {
	//edit the intent
	Bundle b = new Bundle();
	b.putLong("time", newEventDate.getTimeInMillis());
	b.putString("eid", eventHolder.getEid());
	b.putBoolean("editing", editing);
	b.putString("calendarId", eventHolder.getCalendarId());
	this.getIntent().replaceExtras(b);
}

//DATE PICKER THINGS
// Creating dialog


private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
// onDateSet method
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
	//String date_selected = String.valueOf(monthOfYear)+"/"+String.valueOf(dayOfMonth)+"/"+String.valueOf(year);
	
	newEventDate.set(Calendar.MONTH, monthOfYear);
	newEventDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
	newEventDate.set(Calendar.YEAR, year);
	newEventEndDate.set(Calendar.MONTH, monthOfYear);
	newEventEndDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
	newEventEndDate.set(Calendar.YEAR, year);
	
//show the date on the screen
	enterDate.setText(dateFormat.format(newEventDate.getTime()));
	changeIntentTime();

}
};

//Time Picker things

private TimePickerDialog.OnTimeSetListener startTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		
		newEventDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
		newEventDate.set(Calendar.MINUTE, minute);
		newEventEndDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
		newEventEndDate.set(Calendar.MINUTE, minute);
				
		//show the time on the screen
		enterTime.setText(timeFormat.format(newEventDate.getTime()));
		changeIntentTime();
	}
	};
	
private TimePickerDialog.OnTimeSetListener endTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			
			newEventEndDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
			newEventEndDate.set(Calendar.MINUTE, minute);
		
			//show the time on the screen
			enterEndTime.setText(timeFormat.format(newEventEndDate.getTime()));
		}
		};	
	@Override
	protected Dialog onCreateDialog(int id) {
	
	int cyear = newEventDate.get(Calendar.YEAR);
	int cmonth = newEventDate.get(Calendar.MONTH);
	int cday = newEventDate.get(Calendar.DAY_OF_MONTH);
	int chour = newEventDate.get(Calendar.HOUR_OF_DAY);
	int cminute = newEventDate.get(Calendar.MINUTE);
	int ehour = newEventEndDate.get(Calendar.HOUR_OF_DAY);
	int eminute = newEventEndDate.get(Calendar.MINUTE);
	switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener,  cyear, cmonth, cday);
		case START_TIME_DIALOG_ID:
			return new TimePickerDialog(this, startTimeSetListener, chour, cminute, false);
		case END_TIME_DIALOG_ID:
			return new TimePickerDialog(this, endTimeSetListener,ehour, eminute, false);

		}
	return null;
	}

}
